﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.Print.UI
{
    /// <summary>
    /// Interaction logic for SystemConfig.xaml
    /// </summary>
    public partial class SystemConfigUserControl : UserControl
    {
        public SystemConfigUserControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.SetDataToUI();
        }

        private void SetDataToUI()
        {
            string[] printers = PrinterSettings.InstalledPrinters.OfType<string>().ToArray();
            this.cbbDeliveryPrinter.ItemsSource = printers;
            this.cbbGoodsPrinter.ItemsSource = printers;
            this.cbbHotPrinter.ItemsSource = printers;

            this.cbbDeliveryPrinter.SelectedItem = LocalConfigService.GetValue("DeliveryPrinter", "");
            this.cbbGoodsPrinter.SelectedItem = LocalConfigService.GetValue("GoodsPrinter", "");
            this.cbbHotPrinter.SelectedItem = LocalConfigService.GetValue("HotPrinter", "");

            this.chkYTODebug.IsChecked = LocalConfigService.GetValue("YTODebug", "false") == "false" ? false : true;
            this.tbYTOClientId.Text = LocalConfigService.GetValue("YTOClientId", "");
            this.tbYTOClientPass.Text = LocalConfigService.GetValue("YTOClientPass", "");
        }

        private void SetUIToData()
        {
            string deliveryPrinter = this.cbbDeliveryPrinter.Text.Trim();
            string goodsPrinter = this.cbbGoodsPrinter.Text.Trim();
            string hotPrinter = this.cbbHotPrinter.Text.Trim();

            LocalConfigService.UpdateValue("DeliveryPrinter", deliveryPrinter);
            LocalConfigService.UpdateValue("GoodsPrinter", goodsPrinter);
            LocalConfigService.UpdateValue("HotPrinter", hotPrinter);
            LocalConfigService.UpdateValue("YTODebug", this.chkYTODebug.IsChecked.Value.ToString().ToLower());
            LocalConfigService.UpdateValue("YTOClientId", this.tbYTOClientId.Text.Trim());
            LocalConfigService.UpdateValue("YTOClientPass", this.tbYTOClientPass.Text.Trim());
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.SetUIToData();
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCance_Click(object sender, RoutedEventArgs e)
        {
            this.SetDataToUI();
        }
    }
}
